
package ch.hearc.meteo.imp.afficheur.real;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JDialogPreferencesLocal extends JDialog
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public JDialogPreferencesLocal()
		{
		geometry();
		control();
		apparence();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	public boolean isOk()
		{
		return this.isOk;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	private void apparence()
		{

		JPanel panel = (JPanel)this.getContentPane();
		panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setTitle("Propriétés");
		this.setModal(true);
		this.setSize(new Dimension(400, 200));
		this.setResizable(false);
		this.setVisible(true);

		}

	private void control()
		{
		ActionListener validateActionListener = new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent event)
					{
					boolean test = true;

					test = test && txtServerIP.getInputVerifier().verify(txtServerIP);
					test = test && txtServerPort.getInputVerifier().verify(txtServerPort);

					if (test)
						{
						savePreferences();
						JDialogPreferencesLocal.this.dispose();
						}
					}
			};
		txtServerIP.addActionListener(validateActionListener);
		txtServerPort.addActionListener(validateActionListener);

		btnOk.addActionListener(validateActionListener);

		InputVerifier inputVerifier = new InputVerifier()
			{

				@Override
				public boolean verify(JComponent component)
					{
					JTextField txtField = (JTextField)component;

					return !txtField.getText().equals("");
					}
			};
		txtServerIP.setInputVerifier(inputVerifier);
		txtServerPort.setInputVerifier(new InputVerifier()
			{

				@Override
				public boolean verify(JComponent component)
					{
					JTextField txtField = (JTextField)component;
					try
						{
						int value = Integer.valueOf(txtField.getText());
						if (value >= 0 && value <= 65535)
							{
							return true;
							}
						else
							{
							return false;
							}
						}
					catch (Exception e)
						{
						return false;
						}
					}
			});

		}

	private void geometry()
		{
		this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4, 2));
		JLabel lblServerIp = new JLabel("Serveur : ");
		this.txtServerIP = new JTextField();

		this.txtServerIP.setPreferredSize(new Dimension(200, 25));
		JLabel lblServerPort = new JLabel("Port : ");
		this.txtServerPort = new JTextField();
		this.txtServerPort.setPreferredSize(new Dimension(50, 25));
		panel.add(lblServerIp);
		panel.add(txtServerIP);
		panel.add(lblServerPort);
		panel.add(txtServerPort);

		btnOk = new JButton("Ok");

		this.add(Box.createVerticalStrut(10));
		this.add(panel);
		this.add(Box.createVerticalStrut(3));
		this.add(btnOk);
		this.add(Box.createVerticalStrut(10));

		loadPreferences();
		}

	private void loadPreferences()
		{

		try
			{
			Properties p = new Properties();
			p.load(new FileInputStream(PROPERTY_FILENAME));
			this.txtServerIP.setText(p.getProperty(SERVER_IP_PROPERTY_NAME));
			this.txtServerPort.setText(p.getProperty(SERVER_PORT_PROPERTY_NAME));
			this.setLocation(new Point(Integer.valueOf(p.getProperty(POS_X_PROPERTY_NAME)), Integer.valueOf(p.getProperty(POS_Y_PROPERTY_NAME))));

			}
		catch (Exception e)
			{

			}
		}

	private void savePreferences()
		{
		Properties p = new Properties();
		p.setProperty(SERVER_IP_PROPERTY_NAME, txtServerIP.getText());
		p.setProperty(SERVER_PORT_PROPERTY_NAME, txtServerPort.getText());
		p.setProperty(POS_X_PROPERTY_NAME, Integer.toString((int)this.getLocation().getX()));
		p.setProperty(POS_Y_PROPERTY_NAME, Integer.toString((int)this.getLocation().getY()));

		File f = new File(PROPERTY_FILENAME);
		OutputStream out;
		try
			{
			out = new FileOutputStream(f);
			p.store(out, "Meteo station properties");
			}
		catch (Exception e)
			{

			}

		isOk = true;

		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	//Output
	private boolean isOk = false;
	// Tools
	private JTextField txtServerIP;
	private JTextField txtServerPort;

	private JButton btnOk;

	public static final String PROPERTY_FILENAME = "localProperties.ini";
	public static final String SERVER_IP_PROPERTY_NAME = "serverIp";
	public static final String SERVER_PORT_PROPERTY_NAME = "serverPort";
	public static final String POS_X_PROPERTY_NAME = "posX";
	public static final String POS_Y_PROPERTY_NAME = "posY";
	}
